﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AppWPF.Models
{
    class PasswordHelper
    {
        //Propiedades estaticas de solo lectura donde se  le indica a un objeto sin dependecia que ahora tiene dependencia
        public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password",
            typeof(string), typeof(PasswordHelper), new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));

        public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach",
            typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));

        private static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), typeof(PasswordHelper));
        
        // Get And Set de Aderir y contraseña
        public static void SetAttach(DependencyObject dp, bool value)
        {
            dp.SetValue(AttachProperty, value);
        }

        public static bool GetAttach(DependencyObject dp)
        {
            return (bool)dp.GetValue(AttachProperty);
        }

        public static string GetPassword(DependencyObject dp)
        {
            return (string)dp.GetValue(PasswordProperty);
        }

        public static void SetPassword(DependencyObject dp, string value)
        {
            dp.SetValue(PasswordProperty, value);
        }

        public static void SetIsUpdating(DependencyObject dp, bool value)
        {
            dp.SetValue(IsUpdatingProperty, value);
        }

        public static bool GetIsUpdating(DependencyObject dp)
        {
            return (bool)dp.GetValue(IsUpdatingProperty);
        }



        //Evento de Permiso de Aderir datos
        private static void Attach(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox passB = d as PasswordBox;

            if (passB == null)
            {
                return;
            }

            if ((bool)e.OldValue)
            {
                passB.PasswordChanged -= PasswordChanged;
            }

            if ((bool)e.NewValue)
            {
                passB.PasswordChanged += PasswordChanged;
            }
        }

        //Evento cuando se escribe en la caja de texto
        private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox passwordBox = d as PasswordBox;
            passwordBox.PasswordChanged -= PasswordChanged;

            if (!(bool)GetIsUpdating(passwordBox))
            {
                passwordBox.Password = (string)e.NewValue;
            }
            passwordBox.PasswordChanged += PasswordChanged;

        }

        private static void PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox pB = sender as PasswordBox;
            SetIsUpdating(pB, true);
            SetPassword(pB, pB.Password);
            SetIsUpdating(pB, false);
        }
    }
}
